/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Core;

import java.util.List;
import model.Department;
import org.hibernate.Query;
import org.hibernate.Session;
import util.IsisHibernateUtil;

/**
 *
 * @author phinguyen
 */
public class ResearchCore {

    private static Session session;

    public ResearchCore() {
        session = IsisHibernateUtil.getSessionFactory().openSession();
    }

    public Department researchDepartmentById(String id) {
        return (Department) session.get(Department.class, id);
    }

    public Department researchDepartmentByName(String name) {
        for (Department department : this.getAllDepartment()) {
            if (department.getDeptname().equals(name)) {
                return department;
            }
        }
        return null;
    }

    /**
     * return all departments. It will call an HQL query in Hibernate Department's mapping
     * @return
     */
    public List<Department> getAllDepartment() {
        Query query = session.getNamedQuery("HQL_ALL_DEPARTMENT");
        List<Department> listAllDepartment = query.list();
        return listAllDepartment;
    }
}
